package fr.neyb.bernouilli.problem067;

import java.io.InputStreamReader;

import fr.neyb.bernouilli.problem018.Node;
import fr.neyb.bernouilli.problem018.ValuesTriangle;

public class Entry {

	public static void main(String... args) throws Exception {
		Node[][] triangle = ValuesTriangle.parse(
				new InputStreamReader(Entry.class
						.getResourceAsStream("triangle"))).getContent();

		int[][] weight = new int[triangle.length][];
		for (int j = 0; j < weight.length; ++j) {
			weight[j] = new int[j + 1];
		}

		for (int i = triangle.length - 1; i >= 0; --i) {
			for (int j = 0; j <= i; ++j) {
				if (i == triangle.length - 1) {
					weight[i][j] = triangle[i][j].getValue();
				} else {
					weight[i][j] = triangle[i][j].getValue()
							+ Math.max(weight[i + 1][j], weight[i + 1][j + 1]);
				}
			}
		}

		System.out.println(weight[0][0]);
	}
}
